﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using AntUnion.FrameWork.Model.Models;
using Util.Extension;
using Microsoft.AspNetCore.Hosting;
using Util;


namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    public class SystemConfigTabController : BaseController
    {
        private readonly mtContext _context;
        private readonly IWebHostEnvironment hostingEnv;
        public SystemConfigTabController(mtContext context, IWebHostEnvironment env) : base(context, env)
        {
            _context = context;
            hostingEnv = env;
        }
        #region 用户页面
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取用户页面列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, string title, string engtitle, bool? status)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from n in _context.TbSystemConfigTabs.AsNoTracking().Where(k => k.Status == true)
                           select new
                           {
                               id = n.Id,
                               title = n.Title,
                               engtitle = n.EngTitle,
                               status = n.Status
                           };
                if (!string.IsNullOrEmpty(title))
                    data = data.Where(x => x.title.Contains(title.Trim()));
                if (!string.IsNullOrEmpty(engtitle))
                    data = data.Where(x => x.engtitle.Contains(engtitle));
                if (status != null)
                    data = data.Where(x => x.status == status);

                res.Data = await data.OrderByDescending(k => k.id).Skip((page - 1) * limit).Take(limit).ToListAsync();

                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 编辑信息
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            TbSystemConfigTab sg = await _context.TbSystemConfigTabs.FindAsync(id);
            if (sg == null)
            {
                return NotFound();
            }
            else
            {
                return View(sg);
            }

        }

        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Edit(TbSystemConfigTab item)
        {
            Result res = new Result();
            try
            {
                var sg = _context.TbSystemConfigTabs.Where(x => x.Id == item.Id).FirstOrDefault();
                if (sg != null)
                {
                    sg.Title = item.Title;
                    sg.EngTitle = item.EngTitle;
                    sg.Status = item.Status;

                    res.Code = 0;
                    res.Data = await _context.SaveChangesAsync();
                }
                else
                {
                    res.Code = 1;
                    res.Data = false;
                }

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateState(int id, int intState)
        {
            Result res = new Result();
            try
            {
                TbSystemConfigTab sg = await _context.TbSystemConfigTabs.Where(x => x.Id == id).FirstOrDefaultAsync();
                bool state = intState == 1 ? true : false;
                sg.Status = state;
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion
    }
}
